package com.wlyuan.open.application.translator;

import com.wlyuan.core.domain.MappingIds;
import com.wlyuan.core.marker.MappingStatus;
import com.wlyuan.open.domain.api.OpenApi;
import com.wlyuan.open.domain.api.OpenApiInfo;
import com.wlyuan.open.domain.api.utils.MappingMethod;
import com.wlyuan.open.vo.OpenApiCompanyVO;
import lombok.var;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author lijing
 */
public class OpenApiCompanyTranslator {

    @Mapper(uses = {MappingIds.class, MappingStatus.class, MappingMethod.class})
    public interface Translator {

        @Mappings({
                @Mapping(source = "id", target = "apiId")
        })
        OpenApiCompanyVO translate(OpenApi source);

        List<OpenApiCompanyVO> translate(List<OpenApi> source);

    }

    private static final Translator TRANSLATOR = Mappers.getMapper(Translator.class);

    public static List<OpenApiCompanyVO> translate(List<OpenApi> apiList, Map<Long, OpenApiInfo> map) {
        var list = TRANSLATOR.translate(apiList);
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(t -> {
                t.setStatus(-1);
                t.setStatusName("未开通");
                var openApiInfo = map.get(t.getApiId());
                Optional.ofNullable(openApiInfo).ifPresent(apiInfo -> {
                    t.setStatus(apiInfo.getStatus().getValue());
                    t.setStatusName(apiInfo.getStatus().getName());
                    t.setReason(apiInfo.getReason());
                });
            });
        }
        return list;
    }
}
